﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]

namespace Skyworth.CRM.Common
{
    public class Logger
    {
        //private Logger lg = new Logger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private log4net.ILog log = null;

        private static object _synRoot = new Object();

        #region Constructors

        private static Logger _instanceLogger;
        public static Logger Instance
        {
            get
            {
                if (_instanceLogger == null)
                {
                    lock (_synRoot)
                    {
                        if (_instanceLogger == null)
                        {
                            _instanceLogger = new Logger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
                        }
                    }
                }
                return _instanceLogger;
            }
        }

        public Logger(string pName)
        {
            log = log4net.LogManager.GetLogger(pName);
        }

        public Logger(Type pType)
        {
            log = log4net.LogManager.GetLogger(pType);
        }

        public Logger(string pRepository, string pName)
        {
            log = log4net.LogManager.GetLogger(pRepository, pName);
        }

        public Logger(string pRepository, Type pType)
        {
            log = log4net.LogManager.GetLogger(pRepository, pType);
        }

        #endregion

        #region Log methods
        public void Error(Object message)
        {
            if (log.IsErrorEnabled) log.Error(message);
        }

        public void Error(Object message, Exception exception)
        {
            if (log.IsErrorEnabled) log.Error(message, exception);
        }

        public void Fatal(Object message)
        {
            if (log.IsFatalEnabled) log.Fatal(message);
        }

        public void Fatal(Object message, Exception exception)
        {
            if (log.IsFatalEnabled) log.Fatal(message, exception);
        }

        public void Debug(Object message)
        {
            if (log.IsDebugEnabled) log.Debug(message);
        }

        public void Debug(Object message, Exception exception)
        {
            if (log.IsDebugEnabled) log.Debug(message, exception);
        }

        public void Info(Object message)
        {
            if (log.IsInfoEnabled) log.Info(message);
        }

        public void Info(Object message, Exception exception)
        {
            if (log.IsInfoEnabled) log.Info(message, exception);
        }

        public void Warn(Object message)
        {
            if (log.IsWarnEnabled) log.Warn(message);
        }

        public void Warn(Object message, Exception exception)
        {
            if (log.IsWarnEnabled) log.Warn(message, exception);
        }
        #endregion

        #region Log format methods
        public void DebugFormat(string pFormat, Object pArg0)
        {
            if (log.IsDebugEnabled) log.DebugFormat(pFormat, pArg0);
        }

        public void InfoFormat(string pFormat, Object pArg0)
        {
            if (log.IsInfoEnabled) log.InfoFormat(pFormat, pArg0);
        }

        public void WarnFormat(string pFormat, Object pArg0)
        {
            if (log.IsWarnEnabled) log.WarnFormat(pFormat, pArg0);
        }

        public void ErrorFormat(string pFormat, Object pArg0)
        {
            if (log.IsErrorEnabled) log.ErrorFormat(pFormat, pArg0);
        }

        public void FatalFormat(string pFormat, Object pArg0)
        {
            if (log.IsFatalEnabled) log.FatalFormat(pFormat, pArg0);
        }
        #endregion

    }
}
